import JSEncrypt from 'jsencrypt';
import common from '../../assets/common'

const rsa = {}

rsa.encrypt = async function (data) {
  let pubKey = await common.getPublicKey()    // 获取公钥
  let encrypt = new JSEncrypt()               // 新建JSEncrypt对象
  encrypt.setPublicKey(pubKey)                // 设置 加密公钥
  return encrypt.encrypt(data.toString())     // 进行加密
}

rsa.decrypt = function (data, privateKey) {
  // 新建JSEncrypt对象
  let decrypt = new JSEncrypt();
  // 设置私钥
  decrypt.setPrivateKey(privateKey);
  // 解密数据
  return decrypt.decrypt(data);
}
export default rsa
